<?php
require 'dump.inc';
require "URPCurl.php";
require dirname(__FILE__)."/phpQuery/phpQuery.php";
header("Content-Type:text/html;   charset=UTF-8");
$act=$_GET['act'];
function getLibBorrowinfo(){
	//借阅信息
	$xh=$_GET['xh'];
	$vars['name'] = $xh;
	$vars['passwd'] = $_GET['psw'];
	$vars['type'] ='1';

$url = 'http://202.118.72.78/m/reader/check_login.action';
//----------------------------
$nexturl = 'http://202.118.72.78/m/reader/lend_list.action';
$request = new URPCurl($url,$vars,$nexturl);
$content1 = $request->exec();

    $dom = phpQuery::newDocument($content1);
    $list = array();
    $i=-2;
        foreach(pq("#lend_list li") as $item){
            $i++;
            foreach(pq($item)->find("a *") as $li){
            $list[$i][]=urlencode(filt(pq($li)->text()));
            }
        }
        echo urldecode(json_encode($list));
}
function getExamScore(){
	     //-----登录要提交的表单数据---------------
        $vars['zjh'] = $_GET['xh'];
        $vars['mm'] = $_GET['psw'];
              //-------------------------------------
        $url = 'http://202.118.65.20:8083/loginAction.do';
        $nexturl = 'http://202.118.65.20:8083/bxqcjcxAction.do?pageSize=300';

        $request = new URPCurl($url,$vars,$nexturl);
        $content1 = $request->exec();

        $content1 = mb_convert_encoding($content1, "utf-8", "gbk");
        $dom = phpQuery::newDocument($content1);
        $list=array();
        $i=-1;

          foreach(pq(".odd") as $item){
            $i++;
            foreach(pq($item)->find("td") as $td){
              $list[$i][]=urlencode(filt(pq($td)->text()));
            }
          }

          echo urldecode(json_encode($list));

}
function getExamInfo(){
        $vars['zjh'] = $_GET['xh'];
        $vars['mm'] = $_GET['psw'];
              //-------------------------------------
        $url = 'http://202.118.65.20:8083/loginAction.do';
        $nexturl = 'http://202.118.65.20:8083/ksApCxAction.do?oper=getKsapXx';
        $request = new URPCurl($url,$vars,$nexturl);
        $content1 = $request->exec();
        $content1 = mb_convert_encoding($content1, "utf-8", "gbk");
        //echo $content1;
        $dom = phpQuery::newDocument($content1);
        $list=array();
        $i=-1;

          foreach(pq(".odd") as $item){
            $i++;
            foreach(pq($item)->find("td") as $td){
              $list[$i][]=urlencode(filt(pq($td)->text()));
            }
          }
          echo urldecode(json_encode($list));
}
function getLibSearchResult(){
	    $location=$_GET['location'];
        $key=$_GET['key'];
        $url="http://opac.lib.dlut.edu.cn/opac/openlink.php?title={$key}&doctype=ALL&lang_code=ALL&match_flag=forward&displaypg=100&showmode=table&orderby=DESC&sort=CATA_DATE&onlylendable=yes&with_ebook=&location=&location=&location=&location={$location}";
        $content1 = file_get_contents($url);
        
        $dom = phpQuery::newDocument($content1);
        $list = array();
        $i=-1;
        foreach(pq("#result_content tr") as $item){
            $i++;
            foreach(pq($item)->find("td") as $li){
            $list[$i][]=urlencode(filt(pq($li)->text()));
            }
        }
        array_splice($list,0,1);
        echo urldecode(json_encode($list));
}
function getClassTable(){
                  //-----登录要提交的表单数据---------------
    $vars['zjh'] = '20131005173';
    $vars['mm'] = '123456';
                  //-------------------------------------
    $url = 'http://211.103.172.74/loginAction.do';
    $nexturl = 'http://211.103.172.74/xkAction.do?actionType=6';
    $request = new URPCurl($url,$vars,$nexturl);
    $content1 = $request->exec();
    $content1 = mb_convert_encoding($content1, "utf-8", "gbk");
    $content1=get_str($content1,'<td width="50%" align="left" valign="middle">&nbsp;<b>选课结果(已安排时间地点)</b>&nbsp;</td>','<td width="50%" align="left" valign="middle">&nbsp;<b>选课结果列表</b>&nbsp;</td>');
    //echo $content1;
    $data=get_td_array($content1);
    array_splice($data, 0, 3);
    array_splice($data, 5, 1);
    array_splice($data, 11, 1);
    array_splice($data, 15, 2);
    array_splice($data[1], 0, 1);
    array_splice($data[5], 0, 1);
    array_splice($data[11], 0, 1);
    foreach ($data as  &$value) {
      foreach ($value as &$key) {
        $key=urlencode($key);
      }
    }
    $weekday=$_GET['weekday'];
    $weekdayClass = array();
    for ($i=0; $i<15; $i++) { 
      $weekdayClass[]=$data[$i][$weekday];
    }
    //var_dump($weekdayClass);
    echo urldecode(json_encode($weekdayClass));

}

switch ($act){
	case 'score':
		getExamScore();
		break;
	case 'search':
		getLibSearchResult();
		break;
	case 'examinfo':
		getExamInfo();
		break;
	case 'borrow':
		getLibBorrowinfo();
		break;
    case 'classtable':
        getClassTable();
        break;
}